<template>
  <a-card :bordered="false">

    <!-- 查询区域-start -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="refreshTable">
        <a-row :gutter="24">

          <a-col :md="6" :sm="12">
            <a-form-item label="账号">
              <s-input placeholder="输入账号模糊查询" v-model="queryParam.username"></s-input>
            </a-form-item>
          </a-col>

          <a-col :md="6" :sm="8">
            <a-form-item label="性别">
              <a-select v-model="queryParam.sex" placeholder="请选择性别">
                <a-select-option value="1">男性</a-select-option>
                <a-select-option value="2">女性</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>

          <template v-if="toggleSearchStatus">
            <a-col :md="6" :sm="8">
              <a-form-item label="真实名字">
                <a-input placeholder="请输入真实名字" v-model="queryParam.realname"></a-input>
              </a-form-item>
            </a-col>

            <a-col :md="6" :sm="8">
              <a-form-item label="手机号码">
                <a-input placeholder="请输入手机号码查询" v-model="queryParam.phone"></a-input>
              </a-form-item>
            </a-col>

            <a-col :md="6" :sm="8">
              <a-form-item label="用户状态">
                <a-select v-model="queryParam.status" placeholder="请选择">
                  <a-select-option value="1">正常</a-select-option>
                  <a-select-option value="2">冻结</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
          </template>

          <a-col :md="6" :sm="8">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="refreshTable" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
              <a @click="handleToggleSearch" style="margin-left: 8px">
                {{ toggleSearchStatus ? '收起' : '展开' }}
                <a-icon :type="toggleSearchStatus ? 'up' : 'down'"/>
              </a>
            </span>
          </a-col>

        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-end -->

    <!-- 操作按钮区域-start -->
    <div class="table-operator" style="border-top: 5px">
      <a-button v-action="'user:add'" type="primary" icon="plus" @click="handleAdd">添加用户</a-button>
      <a-button type="primary" icon="download" @click="handleExportXls('用户信息')">导出</a-button>
    </div>
    <!-- 操作按钮区域-end -->

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i>已选择&nbsp;<a style="font-weight: 600">{{ selectedRowKeys.length }}</a>项&nbsp;&nbsp;
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <s-table
        ref="table"
        :rowKey="(record) => record.id"
        :columns="columns"
        :data="loadData"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
      >

        <span slot="action" slot-scope="text, record">
          <a @click="handleEdit(record)">编辑</a>

          <a-divider type="vertical"/>

          <a-dropdown>
            <a class="ant-dropdown-link">
              更多 <a-icon type="down"/>
            </a>
            <a-menu slot="overlay">
              <!--
              <a-menu-item>
                <a href="javascript:;" @click="handleDetail(record)">详情</a>
              </a-menu-item>

              <a-menu-item>
                <a href="javascript:;" @click="handleChangePassword(record.username)">密码</a>
              </a-menu-item>

              <a-menu-item>
                <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
                  <a>删除</a>
                </a-popconfirm>
              </a-menu-item>

              <a-menu-item v-if="record.status==1">
                <a-popconfirm title="确定冻结吗?" @confirm="() => handleFrozen(record.id,2,record.username)">
                  <a>冻结</a>
                </a-popconfirm>
              </a-menu-item>

              <a-menu-item v-if="record.status==2">
                <a-popconfirm title="确定解冻吗?" @confirm="() => handleFrozen(record.id,1,record.username)">
                  <a>解冻</a>
                </a-popconfirm>
              </a-menu-item>

              <a-menu-item>
                <a href="javascript:;" @click="handleAgentSettings(record.username)">代理人</a>
              </a-menu-item>
            -->
            </a-menu>
          </a-dropdown>
        </span>
      </s-table>
    </div>
    <!-- table区域-end -->

    <user-modal ref="modalForm" @ok="refreshTable"></user-modal>

    <a-modal
      style="text-align: center"
      v-model="spinningModal"
      :footer="null"
      :centered="true"
      :closable="false"
      :maskClosable="false">
      <a-spin tip="文件生成中，即将开始下载，请稍候..." style="width: 100%" />
    </a-modal>

  </a-card>
</template>

<script>
import { ListMixin } from '@/mixins/ListMixin'
import { apiListUser, apiExportUser } from '@/api/system'
import { apiDownload } from '@/api/download'
import UserModal from './modules/UserModal'

const columns = [
  {
    title: '用户账号',
    align: 'center',
    dataIndex: 'username',
    width: 120
  },
  {
    title: '用户姓名',
    align: 'center',
    width: 100,
    dataIndex: 'realname'
  },
  {
    title: '头像',
    align: 'center',
    width: 120,
    dataIndex: 'avatar',
    scopedSlots: { customRender: 'avatarslot' }
  },

  {
    title: '性别',
    align: 'center',
    width: 80,
    dataIndex: 'sex_dictText',
    sorter: true
  },
  {
    title: '生日',
    align: 'center',
    width: 100,
    dataIndex: 'birthday'
  },
  {
    title: '手机号码',
    align: 'center',
    width: 100,
    dataIndex: 'phone'
  },
  {
    title: '部门',
    align: 'center',
    width: 180,
    dataIndex: 'orgCode'
  },
  {
    title: '状态',
    align: 'center',
    width: 80,
    dataIndex: 'status_dictText'
  },
  {
    title: '操作',
    dataIndex: 'action',
    scopedSlots: { customRender: 'action' },
    align: 'center',
    width: 170
  }
]
export default {
  name: 'UserList',
  mixins: [ListMixin],
  components: {
    UserModal
  },
  data () {
    return {
      columns,
      loadData: parameter => {
        return apiListUser(Object.assign(parameter, this.queryParam)).then(res => {
          return res
        })
      },
      spinningModal: false
    }
  },
  methods: {
    handleAdd () {
      this.$refs.modalForm.show('新增')
    },
    handleEdit: function (record) {
      this.$refs.modalForm.show('编辑', record)
    },
    async handleExportXls () {
      this.spinningModal = true
      const param = { ...this.queryParam }
      const exportRes = await apiExportUser(param)
        .catch(err => {
          this.$message.error('导出失败：' + err.message)
          this.spinningModal = false
        })
      await apiDownload(exportRes.result.fileName).catch(err => {
        this.$message.error('下载失败：' + err.message)
        this.spinningModal = false
      })
      this.spinningModal = false
    }
  }
}
</script>
